package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.AliOssUtil;
import com.sky.utils.JwtUtil;
import com.sky.utils.MyAliUtils;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
public class EmployeeController {
    @Autowired
    MyAliUtils myAliUtils;
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;
    // 分片下载
    @PostMapping("/chunksDown")
    public Result chunksDownFile(){
        return Result.success();
    }
    @PostMapping("/upload")
    public Result uploadFile(
            @RequestParam(value = "file", required = false) MultipartFile file
    ) throws IOException {
        // 获取原始文件名
        if(file == null || file.isEmpty()){
           return Result.success("文件是空的～～");
        }
        String originalFilename = file.getOriginalFilename();
        // 获取文件后缀
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        // 构造新的文件名
        String newFilename = UUID.randomUUID().toString() + substring;
        String img  = myAliUtils.upload(file.getBytes(),newFilename);
        return Result.success(img);
    }

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);
        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

    @PostMapping
    public Result<Employee> addUser(@RequestBody EmployeeDTO emp){
       Result res = employeeService.save(emp);
        return res;
    }

    @GetMapping("/page")
    public Result<PageResult> getPage(EmployeePageQueryDTO employeePageQueryDTO){
        PageResult res = employeeService.pageQuery(employeePageQueryDTO);
        return Result.success(res);
    }
    @PostMapping("/status/{status}")
    public Result setStatus(@PathVariable Integer status,long id){
        employeeService.setStatus(status,id);
        return Result.success("禁用成功");
    }

   @GetMapping("/{id}")
    public Result getById(@PathVariable long id){
        Employee e = employeeService.getById(id);
        return Result.success(e);
    }

    @PutMapping
    public Result updateEmp(@RequestBody EmployeeDTO emp){
        employeeService.updateEmp(emp);
        return Result.success("成功");
    }
}
